<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\controller\Config;
use think\facade\Cache;
use think\facade\View;
use app\common\model\Admin;

class Login
{


    public function index(): string
    {
        //初始化系统参数
        if (!Cache::get('system')) {
            Cache::set('system', Config::lists());
        }
        View::assign('random', rand(1, 7));
        return View::fetch();
    }

    public function check(): \think\response\Json
    {
        return (new Admin())->login(input('post.'));
    }

    public function logout(): \think\response\Json
    {
        try {
            $admin_id = session('admin_id');

            $admin = Admin::find($admin_id);

            session('admin_id', null);
            session('password_token', null);
            \app\common\model\Logs::adds('管理员【' . $admin['admin_name'] . '】退出登录');

            return json(['code' => 1, 'msg' => '已退出']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }


    }

}
